# -*- coding: utf8 -*-
import os
import sys
import datetime
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from book.models import *
from book import utils
import logging

class OrderHomePage(webapp.RequestHandler):
    def get(self):
    	u"""设置当天的收钱人"""            
        manCharge = ''
        users = User.all()            
        todayChairMans = TodayChairMan.gql('WHERE date = :1', datetime.date.today()).fetch(1)
        if todayChairMans:
           for user in users:
               try:
                   if user.key() == todayChairMans[0].user.key():
                       manCharge = user.name
                       break    
               except:
                   pass  
        context = {
                   'current_module' : 'mybooking',
                   'manCharge'   : manCharge
                }

        utils.render_to_response(self.response, "mybooking.html", context)

class OrderPage(webapp.RequestHandler):
    def get(self, emp_id):
        user = User.gql('WHERE code = :1', emp_id).get()
        total_price = 0
        order_datas = []
        if user:
            orders = Order.gql('WHERE user = :1 and date = :2', user, datetime.date.today()).fetch(100)
            for order in orders:
                try:
                    total_price = total_price + order.dish.price
                    order_datas.append(order)
                except:
                    order.delete()
        u"""设置当天的收钱人"""            
        manCharge = ''
        users = User.all()            
        todayChairMans = TodayChairMan.gql('WHERE date = :1', datetime.date.today()).fetch(1)
        if todayChairMans:
           for user2 in users:
               try:
                   if user2.key() == todayChairMans[0].user.key():
                       manCharge = user2.name
                       break    
               except:
                   pass           
        context = {
           'current_module' : 'mybooking',
           'user' : user,
           'orders' : order_datas,
           'total_price' : total_price,
           'manCharge'   : manCharge
        }

        utils.render_to_response(self.response, "mybooking.html", context)
        
class OrderListPage(webapp.RequestHandler):
    def get(self, emp_id):
        user = User.gql('WHERE code = :1', emp_id).get()
        total_price = 0
        orders = []
        if user:
            q = db.Query(Order).filter('user = ', user).order('-date')
            orders = q.fetch(1000)
            for order in orders:
                try:
                    total_price = total_price + order.dish.price
                except:
                    pass
        context = {
           'current_module' : 'mybooking',
           'user' : user,
           'orders' : orders,
           'total_price' : total_price,
           'today' : datetime.date.today()
        }

        utils.render_to_response(self.response, "mybooking.html", context)
        

      

class OrderCancelPage(webapp.RequestHandler):
    def get(self, emp_id, key):
        try:
            order = Order.get(db.Key(key))
            order.delete()
            balances = Balance.gql('WHERE user = :1 and date = :2', order.user,datetime.date.today()).fetch(1)
            if balances:    
                b = balances[0]
                b.balancePay = b.balancePay - order.dish.price    
                b.put()      
        except:
            pass
        
        self.redirect('/mybooking/%s' % emp_id)       